package com.example.myapplication.bean;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.myapplication.R;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.json.JSONException;

import java.util.List;
import com.bumptech.glide.request.RequestOptions;


public class LinearAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //private Context context;
    //消息列表储存消息记录
    private List<MessageReceived> messageList;
    private RequestOptions requestOptions;
    private Activity activity;
    //接收消息列表
    public LinearAdapter(List<MessageReceived> msr, Activity activity){
        messageList = msr;
        this.activity = activity;
        requestOptions = RequestOptions.circleCropTransform()
                .diskCacheStrategy(DiskCacheStrategy.NONE)//不做磁盘缓存
                .skipMemoryCache(true);//不做内存缓存
    }

    //获取子元素类型
    @Override
    public int getItemViewType(int position){

        try {
            int type = messageList.get(position).getMessageType();
            switch (type) {
                //提示消息
                case 1:
                case 2:
                case 3:
                    return R.layout.chat_item_reminder;
                //聊天消息
                case 4:
                    if(messageList.get(position).isSentByMyself()){
                        return R.layout.chat_item_sent;
                    }
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        return R.layout.chat_item;
    }

    class ReminderViewHolder extends RecyclerView.ViewHolder{
        private  TextView reminder;

        public ReminderViewHolder(@NonNull View itemView) {
            super(itemView);

            reminder = itemView.findViewById(R.id.online);
        }


        void bind(MessageReceived message) throws JSONException {
            int type = message.getMessageType();
            if(type == 1){
                reminder.setText(message.getUsername() + "上线了");
            }else if(type == 2){
                reminder.setText(message.getUsername() + "下线了");
            }else{
                reminder.setText("在线用户：" + message.getOnlineUsers());
            }
        }
    }
    class SentViewHolder extends RecyclerView.ViewHolder {
        private TextView senttext;
        private TextView senter_name;
        private ImageView senter;

        public SentViewHolder(@NonNull View itemView) {
            super(itemView);
            //子元素中设置的两个组件
            senttext = itemView.findViewById(R.id.senttext);
            senter_name = itemView.findViewById(R.id.senter_name);
            senter = itemView.findViewById(R.id.senter);
        }

        void bind(MessageReceived message) throws JSONException {
            senttext.setText(message.getTextMessage());
            senter_name.setText(message.getUsername());
            if(!message.getPicture().isEmpty())
                Glide.with(activity).load(message.getPicture()).apply(requestOptions).into(senter);

        }
    }

    class ReceivedViewHolder extends RecyclerView.ViewHolder {
        private TextView text;
        private TextView chat_name;

        private ImageView user;

        public ReceivedViewHolder(@NonNull View itemView) {
            super(itemView);
            //子元素中设置的两个组件
            text = itemView.findViewById(R.id.text);
            chat_name = itemView.findViewById(R.id.chat_name);
            user = itemView.findViewById(R.id.user);
        }

        void bind(MessageReceived message) throws JSONException {
            text.setText(message.getTextMessage());
            chat_name.setText(message.getUsername());
            if(!message.getPicture().isEmpty())
                Glide.with(activity).load(message.getPicture()).apply(requestOptions).into(user);
        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);

        if(viewType == R.layout.chat_item){
            return new ReceivedViewHolder(view);
        } else if(viewType == R.layout.chat_item_sent){
            return new SentViewHolder(view);
        }else {
            return new ReminderViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //数据绑定

                if(messageList != null){
                    MessageReceived messageReceived = messageList.get(position);
                    if (holder.getItemViewType() == R.layout.chat_item_sent) {
                        SentViewHolder sentViewHolder = (SentViewHolder) holder;
                        try {
                            sentViewHolder.bind(messageReceived);
                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }
                    } else if(holder.getItemViewType() == R.layout.chat_item){
                        ReceivedViewHolder receivedViewHolder = (ReceivedViewHolder) holder;
                        try {
                            receivedViewHolder.bind(messageReceived);
                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }
                    } else{
                        ReminderViewHolder reminderViewHolder = (ReminderViewHolder) holder;
                        try {
                            reminderViewHolder.bind(messageReceived);
                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
    }

    @Override
    public int getItemCount() {
        //return 15;
        return messageList.size();
    }
}


